/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.etop.ondoc.beans;

import java.util.Date;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

/**
 * 评论包括评论星级和评论内容<br/>
 * 设置了表示用户权限、记录是否有效的常量
 * @author jansing lz_jansing@163.com
 */
@Entity
@Table(name = "t_comment")
public class Comment {
    public static final int IS_VAILD = 1;
    public static final int IS_NOT_VAILD = 0;
    
    private int id;
    private String context;
    private int stars;
    private Document document;
    private Date createDate;
    /**
     * 1：有效，0：无效
     */
    private int isVaild;
    private User user;

    /**
     * isVaild属性已初始化，<br/>
     * 其他属性都要初始化<br/>
     */
    public Comment() {
        this.isVaild = IS_VAILD;
    }

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "context")
    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

    @Column(name = "stars")
    public int getStars() {
        return stars;
    }

    public void setStars(int stars) {
        this.stars = stars;
    }

    /**
     * 因为评论无需得到它的文档，所以懒加载
     * @return 
     */
    @ManyToOne(fetch = FetchType.LAZY, targetEntity = Document.class)
    @JoinColumn(name = "d_id")
    @Cascade(CascadeType.SAVE_UPDATE)
    public Document getDocument() {
        return document;
    }

    public void setDocument(Document document) {
        this.document = document;
    }

    /**
     * 1：有效，0：无效
     *
     * @return
     */
    @Column(name = "is_vaild")
    public int getIsVaild() {
        return isVaild;
    }

    /**
     * 1：有效，0：无效
     *
     * @param isVaild
     */
    public void setIsVaild(int isVaild) {
        this.isVaild = isVaild;
    }

    @Column(name = "create_date")
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    /**
     * 因为显示评论必然要显示其评论者，所以不懒加载
     *
     * @return
     */
    @ManyToOne(targetEntity = User.class)
    @JoinColumn(name = "u_id")
    @Cascade(CascadeType.SAVE_UPDATE)
    @Fetch(FetchMode.JOIN)
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 59 * hash + Objects.hashCode(this.context);
        hash = 59 * hash + this.stars;
        hash = 59 * hash + Objects.hashCode(this.document);
        hash = 59 * hash + Objects.hashCode(this.createDate);
        hash = 59 * hash + Objects.hashCode(this.user);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Comment other = (Comment) obj;
        if (!Objects.equals(this.context, other.context)) {
            return false;
        }
        if (this.stars != other.stars) {
            return false;
        }
        if (!Objects.equals(this.document, other.document)) {
            return false;
        }
        if (!Objects.equals(this.createDate, other.createDate)) {
            return false;
        }
        if (!Objects.equals(this.user, other.user)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        String s = null;
        try{
            s = "Comment{" + "id=" + id + ", context=" + context + ", stars=" + stars + ", document=" + document.getName() + ", createDate=" + createDate + ", isVaild=" + isVaild + ", user=" + user.getUsername() + '}';
        }catch(Exception e){
            System.err.println("Comment实体类的toString错误，请查看源码");
        }
        return s;
    }

}
